﻿#pragma once
// Copyright (c) lanyeo 2020-2021
// Licenced under MIT license. See https://opensource.org/licenses/MIT for details.
// code style
// 1. use forward declaration (尽可能使用前置声明)
// 2. include file sequence [other project>c>c++>third>self project]
// 

#include <queue>

#include <cppcoro/single_consumer_event.hpp>

#include "task_interface.h"

class CTaskConfParser;

class CTaskManager : public ITaskManager
{
    MANAGER_INFO(ITaskManager)
public:
    explicit CTaskManager();
    ~ CTaskManager() override;
    CTaskManager(CTaskManager& cp) = delete;
    bool Init() override;
    void Update() override;
    void Destroy() override;

    int PushTask(Task&& task) override;
    int PushNetworkTask(Task&& task) override;
    const TaskConf* conf() override;

private:
    void SimpleCoThread();
    cppcoro::task<> SimpleCoMain();
    void NetworkCoThread();
    cppcoro::task<> NetworkCoMain();

private:
    std::thread* m_thread;
    std::thread* m_network_thread;
    std::queue<Task> m_tasks;
    std::queue<Task> m_network_tasks;
    cppcoro::single_consumer_event m_task_wait;
    cppcoro::single_consumer_event m_network_task_wait;
    CTaskConfParser* m_conf_parser;
};
